pro stochastic_sed, imf, metal, age, howmany, nstar, seds=seds
dir='$SLUG_DIR/data/'+metal+'/final/'

a=mrdfits('$SLUG_DIR/data/sed_table_final.fits')
masses=mrdfits(dir+'mass_for_tables.fits')
times=mrdfits(dir+'times_for_tables.fits')

str={metal:metal, age:age, nstar:nstar, $
	star_pdf:fltarr(nstar),$
	sed:fltarr(n_elements(a[0,0,*]))}
str=replicate(str, howmany)

for i=0, howmany-1 do begin
if strmatch(imf, 'Salpeter') then begin
imf_draw,nstar, pdf=star_pdf, $
  min=.08, max=120, alpha=-2.35
endif else print, 'IMF type note supported'
str[i].star_pdf=star_pdf
sed=fltarr(n_elements(a[0,0,*]))
whgood=where(star_pdf GT 0.8, ngood)
if ngood NE 0 then begin
star_pdf=star_pdf[whgood]
for k=0, ngood-1 do begin	;loop over stars
  xind=interpol(findgen(n_elements(times)), times, age)
  yind=interpol(findgen(n_elements(masses)), masses, star_pdf[k])
  sed+=interpolate(a, xind, yind)
endfor
endif
str[i].sed=sed
endfor
seds=str
end
